package com.cheyunkeji.er.view.evaluate;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.cheyun.netsalev3.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/7/12 21:39.
 */

public class TakePhotoDisplayView extends RelativeLayout implements View.OnClickListener {


    private static final String TAG = TakePhotoDisplayView.class.getSimpleName() ;

//   界面展示多个控件时，为其添加下标便于区分，默认为0
    private int itemIndex = 0;

    @BindView(R.id.iv_source)
    ImageView ivSource;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.rl_container)
    RelativeLayout rlContainer;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.rl_content)
    RelativeLayout rootView;

//    照片路径
    private String imgPath = "" ;

    private OnTakePhotoListener listener;

    public TakePhotoDisplayView(Context context) {
        super(context);
    }

    public TakePhotoDisplayView(Context context, AttributeSet attrs) {
        super(context, attrs);

        rootView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.er_take_photo_display_view, this, true);
        ButterKnife.bind(this, rootView);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TakePhotoDisplayView);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            switch (typedArray.getIndex(i)) {
                case R.styleable.TakePhotoDisplayView_photo_background_color:
                    ivSource.setBackgroundColor(typedArray.getColor(typedArray.getIndex(i), Color.parseColor("#00000000")));
                    break;
                case R.styleable.TakePhotoDisplayView_background_img:
                    ivSource.setBackgroundDrawable(typedArray.getDrawable(typedArray.getIndex(i)));
                    break;
                case R.styleable.TakePhotoDisplayView_item_text_desc:
                    tvDesc.setVisibility(View.VISIBLE);
                    tvDesc.setText(typedArray.getString(typedArray.getIndex(i)));
                    break;
                case R.styleable.TakePhotoDisplayView_item_index:
                    itemIndex = typedArray.getInteger(typedArray.getIndex(i), 0);
//                    Log.e(TAG, "TakePhotoDisplayView: ItemIndex : " + itemIndex);
                    break;
                default:
                    break;
            }
        }

        typedArray.recycle();

        initListener();
    }

    private void initListener() {
        rlContainer.setOnClickListener(this);
    }

    public ImageView getImageView() {
        return ivSource;
    }

    public void setImgPath(String path) {
        this.imgPath = path;
    }

    public String getImagePath() {
        return this.imgPath;
    }

    public String getTextDesc(){
        return tvDesc.getText().toString();
    }

    /**
     * 设置拍照按钮状态
     *
     * @param hasPhoto
     */
    public void setPhotoBtnState(boolean hasPhoto) {
        if (hasPhoto) {
            ivIcon.setBackgroundResource(R.mipmap.er_icon_take_photo_pressed);
        } else {
            ivIcon.setBackgroundResource(R.mipmap.er_icon_take_photo_normal);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rl_container && this.listener != null) {
            listener.takePhoto(itemIndex);
        }
    }

    public void setListener(OnTakePhotoListener listener) {
        this.listener = listener;
    }

    public int getItemIndex() {
        return this.itemIndex;
    }

    /**
     * 照片按钮点击时的回调监听
     */
    public interface OnTakePhotoListener {
        void takePhoto(int itemIndex);
    }

}
